﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuatChoi;
namespace Utility
{
    public class GameMessage : BaseMessage
    {
        public GameMessageType Type;
        public Vector ToaDoBatDau;
        public Vector ToaDoKetThuc;
        public string Target;
        public string Comment;
        public GameMessage()
        {
            Type = GameMessageType.KhoiTao;
            ToaDoBatDau = new Vector();
            ToaDoKetThuc = new Vector();
            Target = "";
            Comment = "";
        }

        public GameMessage(byte[] data)
            : this()
        {
            var offset = 0;
            Type = (GameMessageType)BitConverter.ToInt32(data, offset);
            offset += 4;
            ToaDoBatDau.X = BitConverter.ToInt32(data, offset);
            offset += 4;
            ToaDoBatDau.Y = BitConverter.ToInt32(data, offset);
            offset += 4;
            ToaDoKetThuc.X = BitConverter.ToInt32(data, offset);
            offset += 4;
            ToaDoKetThuc.Y = BitConverter.ToInt32(data, offset);
            offset += 4;
            var nameSize = BitConverter.ToInt32(data, offset);
            offset += 4;
            Target = Encoding.UTF8.GetString(data, offset, nameSize);
            offset += nameSize;
            var commentSize = BitConverter.ToInt32(data, offset);
            offset += 4;
            Comment = Encoding.UTF8.GetString(data, offset, commentSize);
        }

        public GameMessage(string data)
            : this(Encoding.UTF8.GetBytes(data))
        { }
        public override byte[] ToByteArray()
        {
            var ls = new List<byte>();
            ls.AddRange(BitConverter.GetBytes((int)Type));
            ls.AddRange(BitConverter.GetBytes(ToaDoBatDau.X));
            ls.AddRange(BitConverter.GetBytes(ToaDoBatDau.Y));
            ls.AddRange(BitConverter.GetBytes(ToaDoKetThuc.X));
            ls.AddRange(BitConverter.GetBytes(ToaDoKetThuc.Y));
            ls.AddRange(BitConverter.GetBytes(Target.Length));
            ls.AddRange(Encoding.UTF8.GetBytes(Target));
            ls.AddRange(BitConverter.GetBytes(Comment.Length));
            ls.AddRange(Encoding.UTF8.GetBytes(Comment));
            return ls.ToArray();
        }

        public override string ToString()
        {
            return Encoding.UTF8.GetString(ToByteArray());
        }
    }
}
